//k1 编辑 2022.2.19

[] spawn A3E_fnc_WriteParamBriefing; //

diag_log format["initPlayerLocal run for %1 (prewaituntil)", name player];

waitUntil {!isNull player};
diag_log format["initPlayerLocal run for %1", name player];

0 spawn {
	disableSerialization;
	waitUntil {!isNull(findDisplay 46)};
	(findDisplay 46) displayAddEventHandler ["keyDown", "_this call a3e_fnc_KeyDown"];
};
titleText ["加载中...", "BLACK",.1];

call compile preprocessFile "Scripts\AT\dronehack_init.sqf";

//waitUntil {!isNil "Escape_Players_enemy"};
if (isNil "Escape_Players_enemy") then {Escape_Players_enemy = []}; //
call A3E_fnc_addUserActions;

player addEventHandler ["HandleRating","_this call A3E_FNC_handleRating;"];

drn_fnc_Escape_DisableLeaderSetWaypoints = {
	if (!visibleMap) exitwith {};
	{player groupSelectUnit [_x, false]} forEach units player;
};

//如果是多人游戏，则禁用作弊“移动到”航路点功能.
if (isMultiplayer) then {
	0 spawn {
		waitUntil {!isNull(findDisplay 46)}; 
		// (findDisplay 46) displayAddEventHandler ["KeyDown","_nil=[_this select 1] call drn_fnc_Escape_DisableLeaderSetWaypoints"];
		(findDisplay 46) displayAddEventHandler ["MouseButtonDown","_nil=[_this select 1] call drn_fnc_Escape_DisableLeaderSetWaypoints"];
	};
};

waitUntil {sleep .1; !isNil "A3E_ParamsParsed" && {!isNil "Player_Server"}};
setPlayerRespawnTime Escape_respawn_time;
ES_ply_name3D = if(ES_ply_name3D == 1)then[{true},{false}];
onEachFrame {
    if (!isNull Player_Server && {isServer || {isNil "Escape_enemySW_side" && {!(Player_Server in Escape_Players_enemy)}}}) then {
        //_textPos = (Player_Server modelToWorldVisual [0,0,0]) vectorAdd [0, 0, 1];
		_str = if(Player_Server getVariable ["Escape_isDSadmin",false])then[{"挂机中，排除在逃离玩家之外"},{"房主挂机中，排除在逃离玩家之外"}];
        drawIcon3D ["", [1,1,1,1], Player_Server modelToWorldVisual [0,0,1], .1, .1, 0, _str, 0, .03];
    };
	{
		if (ES_ply_name3D && {_x distance player < 99} && {_x getVariable ["FAR_isUnconscious", 0] == 0} && {(vehicle _x != vehicle player) || {cameraView == "External"}}) then {
			//_pos = (_x modelToWorldVisual [0,0,0]) vectorAdd [0, 0, 2];
			drawIcon3D ["", [0,.8,0,1], _x modelToWorldVisual [0,0,2], .1, .1, 0, format["%1%2", name _x,if(_x getVariable ["kk_repeatPlyAnim_en", false])then[{" (自动)"},{""}]], 0, .03];
		};
	} forEach (if(isNil "Escape_enemySW_side")then[{playableUnits - Escape_Players_enemy},{Escape_Players_enemy}]) - [player];
	
    if (isNil "Escape_intel_upload" && {!isNil "Escape_intel_find"} && {!isNull Escape_intel_box} && {player distance Escape_intel_box < 2}) then {
		drawIcon3D ["", [1,0,0,1], Escape_intel_box modelToWorldVisual [0,0,0], .5, .5, 0, format["%1s", missionNamespace getVariable ["Escape_intelAllowTake_time", 0]], 0, .03];
    };
	
	if (getMarkerColor "Escape_greenCircle_mak" != "") then {
		drawIcon3D ["", [.85,.85,0,1], markerPos "Escape_greenCircle_mak", .1, .1, 0, "集合！", 0, .03, "PuristaMedium", "center", true];
	};
	if (getMarkerColor "Escape_greenCircle2_mak" != "") then {
		drawIcon3D ["", [.85,.85,0,1], markerPos "Escape_greenCircle2_mak", .1, .1, 0, "集合！", 0, .03, "PuristaMedium", "center", true];
	};
};

player setVariable["A3E_PlayerInitializedLocal",true,true];

[[player], "init_PLY_SV.sqf"] remoteExec ["execVM", 2]; //在服务器执行初始化

kk_initPlayerLocal = 1; //任务开始

waitUntil {sleep .1; (!isNil "A3E_FenceIsCreated" && {!isNil "A3E_StartPos"} && {player getVariable ["A3E_PlayerInitializedServer",false]})};

sleep 1;
diag_log format["Escape debug: %1 is now ready (clientside).", name player];
titleFadeOut 1;
sleep 2;

["Somewhere on", A3E_WorldName , str (date select 2) + "/" + str (date select 1) + "/" + str (date select 0) + " " + str (date select 3) + ":00"] spawn BIS_fnc_infoText;

waitUntil {sleep .5; !isNil("A3E_EscapeHasStarted")};
player setCaptive false;


